/*
 * File     : EnumParameter.java
 * Created  : 22 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.call;

import static com.googlecode.dni.internal.DniTypes.ENUM_DICTIONARY_TYPE;
import static org.objectweb.asm.Opcodes.*;

import java.util.Map;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

import com.googlecode.dni.internal.StructureFieldOrParameter;
import com.googlecode.dni.type.ObjectReference;

/**
 * <p>
 *  Encapsulates a parameter to a native call that comes from a Java enum.
 * </p>
 *
 * @author Matthew Wilson
 */
final class EnumParameter extends Parameter
{

    private String dictionaryField;

    private boolean setDictionaryField;

    {
        // TODO v1.1 technically it's NativeInt
        setNativeType( 'I' );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    int getJavaVarSlots()
    {
        return 1;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void allocateFields( final ClassWriter classWriter, final Map< String, Parameter > createdFields )
    {
        String baseFieldName = "ENUM_DICTIONARY_" + getJavaType().getName().replace( '.', '_' );

        ObjectReference< String > ref = ObjectReference.wrap( (String) null );
        this.setDictionaryField = StructureFieldOrParameter.Helper.findShareableField( createdFields,
                                                                                       this,
                                                                                       baseFieldName,
                                                                                       ref );

        this.dictionaryField = ref.getValue();

        if ( this.setDictionaryField )
        {
            classWriter.visitField( ACC_PRIVATE | ACC_FINAL | ACC_STATIC,
                                    this.dictionaryField,
                                    "L" + ENUM_DICTIONARY_TYPE + ";",
                                    null,
                                    null );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeStaticInitialiserFragment( final MethodVisitor methodVisitor )
    {
        if ( this.setDictionaryField )
        {
            methodVisitor.visitLdcInsn( Type.getType( getJavaType() ) );
            methodVisitor.visitMethodInsn( INVOKESTATIC,
                                           ENUM_DICTIONARY_TYPE,
                                           "get",
                                           "(Ljava/lang/Class;)L" + ENUM_DICTIONARY_TYPE + ";" );
            methodVisitor.visitFieldInsn( PUTSTATIC,
                                          getLibraryImplName(),
                                          this.dictionaryField,
                                          "L" + ENUM_DICTIONARY_TYPE + ";" );
        }
    }

    @Override
    void writeGetParameter( final MethodVisitor methodVisitor )
    {
        methodVisitor.visitFieldInsn( GETSTATIC,
                                      getLibraryImplName(),
                                      this.dictionaryField,
                                      "L" + ENUM_DICTIONARY_TYPE + ";" );
        methodVisitor.visitVarInsn( ALOAD, getJavaVarIndex() );
        methodVisitor.visitMethodInsn( INVOKEVIRTUAL,
                                       ENUM_DICTIONARY_TYPE,
                                       "valueForEnum",
                                       "(Ljava/lang/Enum;)I" );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeReturnParameter( final MethodVisitor methodVisitor,
                               final int returnVarIndex )
    {
        methodVisitor.visitFieldInsn( GETSTATIC,
                                      getLibraryImplName(),
                                      this.dictionaryField,
                                      "L" + ENUM_DICTIONARY_TYPE + ";" );
        methodVisitor.visitVarInsn( ILOAD, returnVarIndex );
        methodVisitor.visitMethodInsn( INVOKEVIRTUAL,
                                       ENUM_DICTIONARY_TYPE,
                                       "enumForValue",
                                       "(I)Ljava/lang/Enum;" );

        methodVisitor.visitTypeInsn( CHECKCAST, getJavaType().getName().replace( '.', '/' ) );

        methodVisitor.visitInsn( ARETURN );
    }

}
